package com.xt.hospital_system.Controller;

import com.alibaba.fastjson2.JSON;
import com.xt.hospital_system.map.map.Cee;
import com.xt.hospital_system.map.map.MdtrtInfo;
import com.xt.hospital_system.map.map.hospital;
import com.xt.hospital_system.map.mapper.MdtrtInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@RestController
public class ParentController {
    @Autowired
    MdtrtInfoMapper mdtrtInfoMapper;

    /**
     * 通过端口来调取所有的病人的信息，Json格式
     *
     * @return http://localhost/api/getAllParent
     */
    @GetMapping("api/getAllParent")
    public String getallparent() {
        return JSON.toJSON(mdtrtInfoMapper.getAllParent()).toString();
    }

    /**
     * 通过前端传递参数来查看是否存在身份证号
     *
     * @param ceertno
     * @return http://localhost/api/getallCeertno
     */
    @GetMapping("/api/getallCeertno")
    public boolean HaveCeerTno(String ceertno) {
        List<Cee> ceertnoList = mdtrtInfoMapper.getAllByCeertnoList();
        for (Cee a :
                ceertnoList) {
            if (ceertno.equals(a.getCEERTNO())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 病人入院填写信息
     * 通过前端返回的值，来增加病人入住院信息
     * PSN_NAME病人的姓名
     * CEERTNO身份证号
     * FIXMEDINS_NAME医院名称
     * BEGIN_DATE  入院时间
     * CREATE_TIME 创建时间 ==  UPDATE_TIME  更新时间
     * <p>
     * http://localhost/api/insert?PSN_NAME=宋江&CEERTNO=23423423&FIXMEDINS_NAME=张三精神病医院&begin_DATE=2023-08-09 18:00:02&create_TIME=2023-08-09 18:00:02
     */
    @GetMapping("/api/insert")
    public boolean insertParent(@RequestParam("PSN_NAME") String PSN_NAME,
                                @RequestParam("CEERTNO") String CEERTNO,
                                @RequestParam("FIXMEDINS_NAME") String FIXMEDINS_NAME,
                                @RequestParam("create_TIME") String begin_DATE,
                                @RequestParam("begin_DATE") String create_TIME) {
        if (!haveCeertno(CEERTNO)) {
            String dateString = "2023-08-23 20:46:32";
            String pattern = "yyyy-MM-dd HH:mm:ss";

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            LocalDateTime BEGIN_date = LocalDateTime.parse(begin_DATE, formatter);
            LocalDateTime CREATE_time = LocalDateTime.parse(create_TIME, formatter);

            ZonedDateTime zonedDateTime1 = BEGIN_date.atZone(ZoneId.systemDefault());
            Instant instant1 = zonedDateTime1.toInstant();
            Date BEGIN_DATE = Date.from(instant1);


            ZonedDateTime zonedDateTime3 = CREATE_time.atZone(ZoneId.systemDefault());
            Instant instant3 = zonedDateTime3.toInstant();
            Date CREATE_TIME = Date.from(instant3);


            MdtrtInfo mdtrtInfo = new MdtrtInfo();
            mdtrtInfo.setPSN_NAME(PSN_NAME);
            mdtrtInfo.setCEERTNO(CEERTNO);
            mdtrtInfo.setFIXMEDINS_NAME(FIXMEDINS_NAME);
            mdtrtInfo.setBEGIN_DATE(BEGIN_DATE);
            mdtrtInfo.setCREATE_TIME(CREATE_TIME);
            mdtrtInfoMapper.insertMdtrtInfo(mdtrtInfo);
            System.out.println(mdtrtInfo.toString() + "信息正在添加...");
            return true;
        }
//

        else if (cando(CEERTNO) & haveCeertno(CEERTNO)) {
            return SetParent(PSN_NAME, CEERTNO, FIXMEDINS_NAME);
        }
        return false;
    }

    /**
     * 病人接下来多次入院修改信息
     * 通过前端返回的值，来修改病人入住院信息
     * PSN_NAME病人的姓名
     * CEERTNO身份证号
     * FIXMEDINS_NAME医院名称
     */
    @GetMapping("/api/update")
    public boolean setParent(String PSN_NAME, String CEERTNO, String FIXMEDINS_NAME) {
        MdtrtInfo mdtrtInfo = new MdtrtInfo();
        mdtrtInfo.setPSN_NAME(PSN_NAME);
        mdtrtInfo.setCEERTNO(CEERTNO);
        Date currentDate = new Date();
        mdtrtInfo.setBEGIN_DATE(currentDate);
        mdtrtInfo.setFIXMEDINS_NAME(FIXMEDINS_NAME);
        return mdtrtInfoMapper.updateMdtrtInfo(mdtrtInfo);

    }

    /**
     * @param PSN_NAME
     * @param CEERTNO
     * @param FIXMEDINS_NAME
     * @return
     */
    public boolean SetParent(String PSN_NAME, String CEERTNO, String FIXMEDINS_NAME) {
        MdtrtInfo mdtrtInfo = new MdtrtInfo();
        mdtrtInfo.setPSN_NAME(PSN_NAME);
        mdtrtInfo.setCEERTNO(CEERTNO);
        Date currentDate = new Date();
        mdtrtInfo.setBEGIN_DATE(currentDate);
        mdtrtInfo.setFIXMEDINS_NAME(FIXMEDINS_NAME);
        return mdtrtInfoMapper.updateMdtrtInfo(mdtrtInfo);

    }

    /**
     * 删除病人信息
     * 只需要前端返回身份证号即可
     * String CEERTNO
     */
    @GetMapping("/api/del")
    public boolean setParent(String CEERTNO) {
        System.out.println("正在删除" + CEERTNO + "身份证号信息");
        return mdtrtInfoMapper.delByCeertnoBool(CEERTNO);
    }

    /**
     * 重写方法来查看是否存在该身份信息
     *
     * @param ceertno
     * @return
     */
    public boolean haveCeertno(String ceertno) {
        List<Cee> ceertnoList = mdtrtInfoMapper.getAllByCeertnoList();
        for (Cee a :
                ceertnoList) {
            if (ceertno.equals(a.getCEERTNO())) {
                return true;
            }
        }
        return false;
    }

    @GetMapping("/api")
    public String api() {
        return "医院系统_后端接口";
    }

    /**
     * 退园，然后返回时间
     * 布尔类型
     *
     * @param cc
     * @return localhost/api/out?cc=23423423
     */
    @GetMapping("/api/out")
    public boolean out(String cc) {
        if (haveCeertno(cc)) {
            Date currentDate = new Date();
            System.out.println(cc + "正在办理退院");
            mdtrtInfoMapper.setout(cc, currentDate);
            return true;
        } else {
            return false;
        }


    }

    /**
     * 查看是否出院
     *
     * @param CEERTNO
     * @return
     */
//    @GetMapping("/api/CanDo")
    public boolean cando(String CEERTNO) {
        System.out.println(CEERTNO);
//        Date createDate =mdtrtInfoMapper.Compare(CEERTNO).get(0).getCREATE_TIME();
//        Date endDate=mdtrtInfoMapper.Compare(CEERTNO).get(0).getEND_TIME();
//        return endDate.compareTo(createDate) >= 0;
        try {
            List<MdtrtInfo> allParent = mdtrtInfoMapper.getAllParent();
            for (MdtrtInfo m :
                    allParent) {
                if (m.getCEERTNO().equals(CEERTNO)) {
                    int comparison = m.getBEGIN_DATE().compareTo(m.getEND_DATE());

                    if (comparison < 0) {
                        return true;
                    } else if (comparison > 0) {
                        return false;
                    } else {
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            return false;

        }
        return false;

    }

    @GetMapping("/api/getallHost")
    public String getAllHost() {
        List<hospital> allHost = mdtrtInfoMapper.getallHost();
        return JSON.toJSON(allHost).toString();

    }


    @GetMapping("/api/insertHost")
    public boolean inserHost(String name) {
        List<hospital> allHost = mdtrtInfoMapper.getallHost();
        for (hospital h :
                allHost) {
            if (h.getName().equals(name)) {
                return false;
            }
        }
        return mdtrtInfoMapper.insertAllHost(name);
    }

}
